<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Slideshow extends Admin_Controller {
	
	public function __construct(){
		parent::__construct();
        $this->load->model(array('mod_slideshow'));
	}

	public function index()
	{
		$this->view();
	}
	
	public function view(){
        $config['base_url'] = base_url() . 'slideshow/slideshow/view/';
        $config['total_rows'] = $this->mod_slideshow->getSlideshowNum();
		$config['uri_segment'] = 4;
        $config['per_page'] = 25;

        $this->pagination->initialize($config);
		$data['data'] = $this->mod_slideshow->getSlideshow($this->uri->segment(4), $config['per_page']);
				
		$data['title']="Slidesow Management";
		$data['page']='slideshow/view';
		$data['action']='Slidesow Management';
    	$this->load->view('layout/admin',$data);
	}
	
	public function add(){
					
		if($this->input->post()){
			$image = $this->input->post('image');
	
			$config['upload_path'] = SLIDESHOW_IMAGE_PATH;
			$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
			$config['max_size']	= '1024';
			$config['max_width'] = '990';
			$config['max_height'] = '300';
		
			$this->load->library('upload', $config);
	
			if ($this->upload->do_upload('image')){				
				$image = $this->upload->data();
				$imageName = $image['file_name'];
				resize_image($imageName, 40, NULL, $config['upload_path']);	// 40 x 130
					
				if($this->mod_slideshow->insert($imageName) > 0){
					$this->session->set_userdata('ms', 'Successfully add new slideshow!');
					redirect('slideshow/slideshow/view');
				} else {
                    $this->session->set_userdata('ms', 'Could not add new slideshow, please try again!');
                }
			} else {
				$this->session->set_userdata('ms', $this->upload->display_errors());
			}
		}
		
		$data['title']="Add New Slidesow";
		$data['page']='slideshow/new';
		$data['action']='Add New Slidesow';
    	$this->load->view('layout/admin',$data);
	}
	
    public function delete(){
		$id = $this->uri->segment(3);
		$data = $this->mod_slideshow->getSlideshowById($id);
		
        if ($this->mod_slideshow->delete($id) > 0) {
            $this->session->set_userdata('ms', 'Successfully delete!');
			delete_file(SLIDESHOW_IMAGE_PATH . $data[field('image')]);
			delete_file(SLIDESHOW_IMAGE_PATH . 'thumbs/' . $data[field('image')]);
        }else{
            $this->session->set_userdata('ms', 'Could not delete, please try again!');
		}
        redirect('slideshow/slideshow/view');
    }
}
